﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Gladkikh.Sims2.Combiner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
   			buttonAuto.Image = Gladkikh.Sims2.Combiner.Properties.Resources.logo_5;
   			buttonGenerate.Image = Gladkikh.Sims2.Combiner.Properties.Resources.logo_5;
   			buttonExit.Image = Gladkikh.Sims2.Combiner.Properties.Resources.logo_5;
   			toolStripMenuItemDeepSearch.Image = Gladkikh.Sims2.Combiner.Properties.Resources.search;
   			  
   			Program.sc.label3.Text += "форма инициализирована ";
   			Application.DoEvents();
   			  
        }

        Database.Sims2Mdb mdb;

        private void Form1_Load(object sender, EventArgs e)
        {
        	Program.sc.label3.Text += "и загружена...открываем БД";
        	Application.DoEvents();
            LoadFromMDB();

            
            addonsFlowPanel1.OnSearchComplete += new EventHandler(addonsFlowPanel1_OnSearchComplete);
            addonsFlowPanel1.OnSearchBegin += new EventHandler(addonsFlowPanel1_OnSearchBegin);
            Program.sc.CloseIt();
        }

        void addonsFlowPanel1_OnSearchBegin(object sender, EventArgs e)
        {
            Enabled = false;
        }

        void addonsFlowPanel1_OnSearchComplete(object sender, EventArgs e)
        {
            this.Enabled = true;
        }

        private void LoadFromMDB()
        {
            SuspendLayout();

            mdb = new Database.Sims2Mdb();
            mdb.InitConnection();
         
            List<Database.Sims2Addon> addons;
            mdb.Read(out addons);
            addonsFlowPanel1.Addons = addons;
            addonsFlowPanel1.Size = new Size(addonsFlowPanel1.Width, addonsFlowPanel1.RecommendedHeight);
            this.Size = new Size(this.Width, Height - (addonsFlowPanel1.Height - addonsFlowPanel1.RecommendedHeight));
            ResumeLayout(false);
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonAuto_Click(object sender, EventArgs e)
        {
            addonsFlowPanel1.SearchIt();
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            addonsFlowPanel1.BuildRegistry();

            if(PreviewToolStripMenuItem.Checked)
            {
                Preview pr = new Preview();
                pr.Show();
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdb.Write(addonsFlowPanel1.Addons);
            Properties.Settings.Default.PreviewReg = PreviewToolStripMenuItem.Checked;
            Properties.Settings.Default.Save();
        }


        private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)
        {
           AboutBox1 ab = new AboutBox1();
           ab.Show();
        }

        private void записатьБДToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mdb.Write(addonsFlowPanel1.Addons);
        }

        private void перечитатьБДToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadFromMDB();
        }

        private void toolStripMenuItemDeepSearch_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                addonsFlowPanel1.SearchForAddon(folderBrowserDialog1.SelectedPath, 100);
        }

        private void toolStripMenuItemClear_Click(object sender, EventArgs e)
        {
            addonsFlowPanel1.ClearExePaths();
            addonsFlowPanel1.ReBuildFlow();
        }

    }
}
